<!--  -->
<template>
  <div>
    <el-dialog v-model="visible" title="" :before-close="() => close()">
      <el-form :model="form">
        <el-form-item label="账号">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.nickName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.note" autocomplete="off" />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-radio-group v-model="form.status">
          <el-radio :label="1" size="large">是</el-radio>
          <el-radio :label="0" size="large">否</el-radio>
        </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submit">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, watch } from "vue";
import { umsAdmin } from "../../../../request/http";
import { ElMessage, Message } from "element-plus";

const props = withDefaults(
  defineProps<{
    visible: boolean;
    formData: AdminResDataObj;
  }>(),
  {
    visible: false,
    formData: () => ({} as AdminResDataObj),
  }
);

const emit = defineEmits<{
  (event: "close", r?: "reload"): void;
}>();

const form = ref({} as AdminResDataObj);
watch(
  () => props.formData,
  (val) => {
    form.value = { ...val };
  }
);

// 确定按钮
async function submit() {
  let res;
  let msg;
  if(form.value.id){
    res = await umsAdmin.getAdminUpdate(form.value);
    msg = "修改用户信息成功";
  }else{
    res = await umsAdmin.registerAdmin(form.value);
    msg = "添加用户信息成功";
  }
  if (res.code === 200) {
    close("reload");
    ElMessage({
      message:msg,
      type: "success",
    });
  }
}
function close(r?: "reload") {
  // 关闭弹框
  emit("close", r);
}
</script>
<style lang="less" scoped></style>
